package com.ruoyi.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.system.domain.vo.ArchiveStrategyVo;
import com.ruoyi.system.domain.bo.ArchiveStrategyBo;
import com.ruoyi.system.service.IArchiveStrategyService;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;

import java.util.List;
import java.util.Arrays;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.servlet.http.HttpServletResponse;

/**
 * 归档策略控制器
 * 前端访问路由地址为:/system/strategy
 *
 * @author ruoyi
 * @date 2024-12-06
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/strategy")
public class ArchiveStrategyController extends BaseController {

    private final IArchiveStrategyService iArchiveStrategyService;

    /**
     * 查询归档策略列表
     */
    @SaCheckPermission("system:strategy:list")
    @GetMapping("/list")
    public TableDataInfo<ArchiveStrategyVo> list(ArchiveStrategyBo bo, PageQuery pageQuery) {
        return iArchiveStrategyService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出归档策略列表
     */
    @SaCheckPermission("system:strategy:export")
    @Log(title = "归档策略", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ArchiveStrategyBo bo, HttpServletResponse response) {
        List<ArchiveStrategyVo> list = iArchiveStrategyService.queryList(bo);
        ExcelUtil.exportExcel(list, "归档策略", ArchiveStrategyVo.class, response);
    }

    /**
     * 获取归档策略详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:strategy:query")
    @GetMapping("/{id}")
    public R<ArchiveStrategyVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iArchiveStrategyService.queryById(id));
    }

    /**
     * 新增归档策略
     */
    @SaCheckPermission("system:strategy:add")
    @Log(title = "归档策略", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ArchiveStrategyBo bo) {
        return toAjax(iArchiveStrategyService.insertByBo(bo));
    }

    /**
     * 修改归档策略
     */
    @SaCheckPermission("system:strategy:edit")
    @Log(title = "归档策略", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ArchiveStrategyBo bo) {
        return toAjax(iArchiveStrategyService.updateByBo(bo));
    }

    /**
     * 删除归档策略
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:strategy:remove")
    @Log(title = "归档策略", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iArchiveStrategyService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    @GetMapping("/config")
    public R<List> getConfig() {
        return R.ok(iArchiveStrategyService.getConfig());
    }

    @PostMapping("/check")
    public R<Void> check(@RequestBody ArchiveStrategyBo bo) {
        return toAjax(iArchiveStrategyService.checkStrategy(bo));
    }
}
